<?php

/**
 * LEAGUEMan
 * 
 * Copyright (C) 2007 Marko Bischof
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
abstract class Leaguetable
{
    protected $rows;
    protected $project;
    protected $results;
    protected $teams;
    protected $excludes;

    public function __construct(LM_Table_Project $project)
    {

        $this->rows = array();
        $this->project = $project;
        $this->results = array();
        $this->teams = $project->getTeams("team_id");
        $this->excludes = $project->getParam("excludes", array());


        foreach ($this->teams AS $team) {
            if (!in_array($team->team_id, $this->excludes)) {
                $this->rows[$team->team_id] = new Leaguetablerow($team);
            }
        }
    }

    abstract public function compare(Leaguetablerow $a, Leaguetablerow $b);

    public function setResults($results = array())
    {
        $this->results = $results;
    }

    public function getTable($mode = 0)
    {

        foreach ($this->results AS $result) {

            $row1 = $this->getRow($result->team1_id);
            $row2 = $this->getRow($result->team2_id);

            if ($row1 != null && $row2 != null) {
                if ($mode == 0) { //Total
                    $this->updateRow($row1, $result->e1, $result->e2, $result->type);
                    $this->updateRow($row2, $result->e2, $result->e1, $result->type);
                } else if ($mode == 1) { //Home
                    $this->updateRow($row1, $result->e1, $result->e2, $result->type);
                } else if ($mode == 2) { //Away
                    $this->updateRow($row2, $result->e2, $result->e1, $result->type);
                }
            }
        }

        uasort($this->rows, array($this, "compare"));
        return $this->rows;
    }

    public function getRow($teamid)
    {

        if (isset($this->rows[$teamid])) {
            return $this->rows[$teamid];
        } else {
            return null;
        }
    }

    public function updateRow($row, $e1, $e2, $type = 0)
    {

        if ($e1 > $e2) {
            switch ($type) {
                case 0:
                    $row->incV1();
                    $points = $this->project->getParam("points_after_regular_time_v");
                    $points2 = $this->project->getParam("points_after_regular_time_d");
                    break;

                case 1:
                    $row->incV2();
                    $points = $this->project->getParam("points_after_extra_time_v");
                    $points2 = $this->project->getParam("points_after_extra_time_d");
                    break;

                case 2:
                    $row->incV3();
                    $points = $this->project->getParam("points_after_penalty_v");
                    $points2 = $this->project->getParam("points_after_penalty_d");
                    break;
            }
        }

        if ($e1 == $e2) {
            switch ($type) {
                case 0:
                    $row->incT1();
                    $points = $this->project->getParam("points_after_regular_time_t");
                    $points2 = $this->project->getParam("points_after_regular_time_t");
                    break;

                case 1:
                    $row->incT2();
                    $points = $this->project->getParam("points_after_extra_time_t");
                    $points2 = $this->project->getParam("points_after_extra_time_t");
                    break;

                case 2:
                    $row->incT3();
                    $points = $this->project->getParam("points_after_penalty_t");
                    $points2 = $this->project->getParam("points_after_penalty_t");
                    break;
            }
        }

        if ($e1 < $e2) {
            switch ($type) {
                case 0:
                    $row->incD1();
                    $points = $this->project->getParam("points_after_regular_time_d");
                    $points2 = $this->project->getParam("points_after_regular_time_v");
                    break;

                case 1:
                    $row->incD2();
                    $points = $this->project->getParam("points_after_extra_time_d");
                    $points2 = $this->project->getParam("points_after_extra_time_v");
                    break;

                case 2:
                    $row->incD3();
                    $points = $this->project->getParam("points_after_penalty_d");
                    $points2 = $this->project->getParam("points_after_penalty_v");
                    break;
            }
        }

        $row->incMatches();
        $row->addScore1($e1);
        $row->addScore2($e2);
        $row->addPoints($points);
        $row->addPoints2($points2);
    }

}

class Leaguetablerow
{
    protected $team;
    protected $last;
    protected $cnt_v;
    protected $cnt_v_after_extra;
    protected $cnt_v_after_penalty;
    protected $cnt_t;
    protected $cnt_t_after_extra;
    protected $cnt_t_after_penalty;
    protected $cnt_d;
    protected $cnt_d_after_extra;
    protected $cnt_d_after_penalty;
    protected $cnt_matches;
    protected $score1;
    protected $score2;
    protected $points;
    protected $points2;

    public function __construct($team)
    {

        $this->team = $team;

        $this->last = 0;

        $this->cnt_v = 0;
        $this->cnt_v_after_extra = 0;
        $this->cnt_v_after_penalty = 0;

        $this->cnt_t = 0;
        $this->cnt_t_after_extra = 0;
        $this->cnt_t_after_penalty = 0;

        $this->cnt_d = 0;
        $this->cnt_d_after_extra = 0;
        $this->cnt_d_after_penalty = 0;

        $this->cnt_matches = 0;
        $this->score1 = 0;
        $this->score2 = 0;

        $this->points = 0;
    }

    public function getTeam()
    {
        return $this->team;
    }

    public function setLast($last)
    {
        $this->last = $last;
    }

    public function incMatches()
    {
        $this->cnt_matches++;
    }

    public function incV1()
    {
        $this->cnt_v++;
    }

    public function incV2()
    {
        $this->cnt_v_after_extra++;
    }

    public function incV3()
    {
        $this->cnt_v_after_penalty++;
    }

    public function incT1()
    {
        $this->cnt_t++;
    }

    public function incT2()
    {
        $this->cnt_t_after_extra++;
    }

    public function incT3()
    {
        $this->cnt_t_after_penalty++;
    }

    public function incD1()
    {
        $this->cnt_d++;
    }

    public function incD2()
    {
        $this->cnt_d_after_extra++;
    }

    public function incD3()
    {
        $this->cnt_d_after_penalty++;
    }

    public function addScore1($score1)
    {
        $this->score1 = $this->score1 + $score1;
    }

    public function addScore2($score2)
    {
        $this->score2 = $this->score2 + $score2;
    }

    public function addPoints($points)
    {
        $this->points = $this->points + $points;
    }

    public function addPoints2($points2)
    {
        $this->points2 = $this->points2 + $points2;
    }

    public function getV1()
    {
        return $this->cnt_v;
    }

    public function getV2()
    {
        return $this->cnt_v_after_extra;
    }

    public function getV3()
    {
        return $this->cnt_v_after_penalty;
    }

    public function getT1()
    {
        return $this->cnt_t;
    }

    public function getT2()
    {
        return $this->cnt_d_after_extra;
    }

    public function getT3()
    {
        return $this->cnt_d_after_penalty;
    }

    public function getD1()
    {
        return $this->cnt_d;
    }

    public function getD2()
    {
        return $this->cnt_d_after_extra;
    }

    public function getD3()
    {
        return $this->cnt_d_after_penalty;
    }

    public function getMatches()
    {
        return $this->cnt_matches;
    }

    public function getScore1()
    {
        return $this->score1;
    }

    public function getScore2()
    {
        return $this->score2;
    }

    public function getPoints()
    {
        return $this->points;
    }

    public function getPoints2()
    {
        return $this->points2;
    }

    public function getDiff()
    {
        return $this->score1 - $this->score2;
    }

    public function getScores()
    {
        return sprintf("%s:%s", $this->getScore1(), $this->getScore2());
    }

    public function getPoints2Points()
    {
        return sprintf("%s:%s", $this->getPoints(), $this->getPoints2());
    }

}

class Leaguetable_Football extends Leaguetable
{

    public function compare(Leaguetablerow $a, Leaguetablerow $b)
    {

        $i = $b->getPoints() - $a->getPoints();
        if ($i == 0)
            $i = $b->getDiff() - $a->getDiff();
        if ($i == 0)
            $i = $b->getScore1() - $a->getScore1();
        if ($i == 0) {

            $e1 = 0;
            $e2 = 0;
            foreach ($this->results AS $result) {

                $t1 = $a->getTeam()->team_id;
                $t2 = $b->getTeam()->team_id;

                if ($result->team1_id == $t1 && $result->team2_id == $t2) {
                    $e1 = $e1 + $result->e1;
                    $e2 = $e2 + $result->e2;
                }

                if ($result->team1_id == $t2 && $result->team2_id == $t1) {
                    $e1 = $e1 + $result->e2;
                    $e2 = $e2 + $result->e1;
                }
            }

            $i = $e2 - $e1;
        }

        return $i;
    }

}

class Leaguetable_Special extends Leaguetable_Football
{
    protected $filter;

    /**
     * 	eg: teammap->teammap_info north|south
     */
    public function setFilter($filter)
    {
        $this->filter = $filter;
    }

    public function getTable($mode = 0)
    {

        foreach ($this->results AS $result) {

            $row1 = $this->getRow($result->team1_id);
            $row2 = $this->getRow($result->team2_id);

            if ($row1 != null && $row2 != null) {
                if ($mode == 0) { //Total
                    $this->updateRow($row1, $result->e1, $result->e2, $result->type);
                    $this->updateRow($row2, $result->e2, $result->e1, $result->type);
                } else if ($mode == 1) { //Home
                    $this->updateRow($row1, $result->e1, $result->e2, $result->type);
                } else if ($mode == 2) { //Away
                    $this->updateRow($row2, $result->e2, $result->e1, $result->type);
                }
            }
        }

        $rows = $this->rows;
        foreach ($rows AS $row) {
            $teamid = $row->getTeam()->team_id;

            if ($row->getTeam()->teammap_info != $this->filter) {
                unset($rows[$teamid]);
            }
        }

        uasort($rows, array($this, "compare"));
        return $rows;
    }

}